package ltd.hxya.novel.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hxya.novel.book.dto.NewsInfoRespDto;
import ltd.hxya.novel.book.entity.NewsInfo;
import ltd.hxya.novel.book.mapper.NewInfoMapper;
import ltd.hxya.novel.book.service.NewsInfoService;
import ltd.hxya.novel.book.utils.constant.DatabaseConsts;
import ltd.hxya.novel.book.utils.resp.RestResp;
import ltd.hxya.novel.common.bean.Result;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 首页的资讯的处理过程
 * @author ZWW
 * @since 2022-11-16
 */
@Service
public class NewsInfoServiceImpl extends ServiceImpl<NewInfoMapper, NewsInfo> implements NewsInfoService {

    /**
     * 最新新闻列表查询，并放入缓存中
     * 先查redis中是否存在，然后在进行数据库的查找处理
     * 广告的信息少，这里的id是不需要进行改处理的
     * @return
     */
    @Override
    public Result<List<NewsInfoRespDto>> listLatestNews() {
        // 从新闻信息表中查询出最新发布的两条新闻
        QueryWrapper<NewsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(DatabaseConsts.CommonColumnEnum.CREATE_TIME.getName())
                .last(DatabaseConsts.SqlEnum.LIMIT_2.getSql());
        List<NewsInfoRespDto> collect = baseMapper.selectList(queryWrapper).stream().map(v -> NewsInfoRespDto.builder()
                .id(v.getId())
                .categoryId(v.getCategoryId())
                .categoryName(v.getCategoryName())
                .title(v.getTitle())
                .sourceName(v.getSourceName())
                .updateTime(v.getUpdateTime())
                .build()).collect(Collectors.toList());
        return Result.success(collect);
    }
}
